package com.lhkj.iams.framework.webservice.config;

import com.lhkj.iams.framework.webservice.interceptor.AuthInterceptorJAWS;
import com.lhkj.iams.framework.webservice.service.ProspectService;
import com.lhkj.iams.framework.webservice.service.impl.ProspectServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * http://localhost:8081/cxf/prosService?wsdl  访问链接必须加上wsdl否则会抛出异常
 */
@Configuration
public class CxfConfig {
    @Bean
    public ProspectService myService() {
        return new ProspectServiceImpl();
    }
	//CXFServlet()用来拦截cxf，拦截路径/cxf/*
    @Bean
    public ServletRegistrationBean createServletRegistrationBean(){
        return new ServletRegistrationBean(new CXFServlet(),"/cxf/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return new SpringBus();
    }

    @Bean
    @Qualifier("prosService")
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(),myService());
        endpoint.getInInterceptors().add(new AuthInterceptorJAWS());//添加校验拦截器
        endpoint.publish("/prosService"); //暴露的api
        return endpoint;
    }
    @Bean("jsonProvider") // 构造一个json转化bean，因为后面需要用这个bean配置json转化，所以给他取个名
    public JacksonJsonProvider getJacksonJsonProvider() {
        return new JacksonJsonProvider();
    }
}
